package com.ld.control.utils;

import com.alibaba.fastjson.JSONObject;
import com.ld.control.common.ResultResponse;
import com.ld.control.sysenum.ResultResponseEnum;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.PrintWriter;

@Slf4j
public class ResponseUtils {

    public static void returnErrorInfo(HttpServletResponse response, ResultResponseEnum resultResponseEnum) {
        response.setContentType("application/json; charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        try {
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            ResultResponse<?> error = ResultResponse.error(resultResponseEnum);
            String jsonString = JSONObject.toJSONString(error);
            response.getWriter().write(jsonString);
        } catch (IOException e) {
            log.error("response error", e);
        }/* finally {
            if (servletOutputStream != null)
                servletOutputStream.close();
        }*/
    }
}
